package org.jeecg.modules.demo.report3.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.report1.entity.Columns;
import org.jeecg.modules.demo.report1.entity.FreeType;
import org.jeecg.modules.demo.report1.entity.Xin;
import org.jeecg.modules.demo.report1.entity.Yingshou;
import org.jeecg.modules.demo.report1.mapper.GjshYingshouMapper;
import org.jeecg.modules.demo.report1.service.IGjshYingshouService;
import org.jeecg.modules.demo.report1.service.IYingshouService;
import org.jeecg.modules.demo.report3.entity.Qianshou;
import org.jeecg.modules.demo.report3.service.IQianshouService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 应收报表
 * @Author: jeecg-boot
 * @Date:   2024-04-01
 * @Version: V1.0
 */
@Api(tags="应收报表")
@RestController
@RequestMapping("/report3/qianshou")
@Slf4j
public class QianshouController extends JeecgController<Qianshou, IQianshouService> {
	@Autowired
	private IQianshouService qianshouService;
	@Autowired
	private GjshYingshouMapper gjshYingshouMapper;
	@Autowired
	private IGjshYingshouService gjshYingshouService;
	 @Autowired
	 private IYingshouService yingshouService;
	/**
	 * 分页列表查询
	 *
	 * @param qianshou
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "应收报表-分页列表查询")
	@ApiOperation(value="应收报表-分页列表查询", notes="应收报表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<Xin>> queryPageList(Qianshou qianshou,
											@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
											HttpServletRequest req) {
		QueryWrapper<Qianshou> queryWrapper = QueryGenerator.initQueryWrapper(qianshou, req.getParameterMap());
		Page<Qianshou> page = new Page<Qianshou>(pageNo, pageSize);
		IPage<Qianshou> pageList = qianshouService.page(page, queryWrapper);

		//行转列
		//当前社区的全部表头
		List<FreeType> columnTableList = gjshYingshouMapper.selectColumns(qianshou.getCommunityName());
		//获取动态列
		List<Columns> columns = gjshYingshouService.getColumns(qianshou.getCommunityName(), columnTableList);
		//获取动态数据
		List<Map<String,Object>> resultDataSource = gjshYingshouService.getResultDataSource(qianshou.getCommunityName(), columnTableList, columns);
		//计算表列下的合计
		Map bigDecimalList = gjshYingshouService.getResultTotal(resultDataSource, columnTableList);
		Xin zong = new Xin();
		//设置动态表头和数据
		zong.setColumnsList(columns);
		zong.setResultData(resultDataSource);
		zong.setResultTotal(bigDecimalList);
		return Result.OK(zong);
	}

	/**
	 *   添加
	 *
	 * @param qianshou
	 * @return
	 */
	@AutoLog(value = "应收报表-添加")
	@ApiOperation(value="应收报表-添加", notes="应收报表-添加")
	@RequiresPermissions("report3:qianshou:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Qianshou qianshou) {
		qianshouService.save(qianshou);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param qianshou
	 * @return
	 */
	@AutoLog(value = "应收报表-编辑")
	@ApiOperation(value="应收报表-编辑", notes="应收报表-编辑")
	@RequiresPermissions("report3:qianshou:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Qianshou qianshou) {
		qianshouService.updateById(qianshou);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "应收报表-通过id删除")
	@ApiOperation(value="应收报表-通过id删除", notes="应收报表-通过id删除")
	@RequiresPermissions("report3:qianshou:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		qianshouService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "应收报表-批量删除")
	@ApiOperation(value="应收报表-批量删除", notes="应收报表-批量删除")
	@RequiresPermissions("report3:qianshou:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.qianshouService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "应收报表-通过id查询")
	@ApiOperation(value="应收报表-通过id查询", notes="应收报表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Qianshou> queryById(@RequestParam(name="id",required=true) String id) {
		Qianshou qianshou = qianshouService.getById(id);
		if(qianshou==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(qianshou);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param qianshou
    */
    @RequiresPermissions("report3:qianshou:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Qianshou qianshou) {
        return super.exportXls(request, qianshou, Qianshou.class, "应收报表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("report3:qianshou:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Qianshou.class);
    }

}
